<template>
	<view class="content">
		<view class="menu-block">
			<view class="title">
				个人信息
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">联系人</view>
				</view>
				<input v-model="form.contacts" placeholder="请输入联系人姓名" class="info-crl" maxlength="10" type="text" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">联系电话</view>
				</view>
				<input v-model="form.phone" placeholder="请输入联系电话" class="info-crl" maxlength="11" type="number" />
			</view>
		</view>
		<view class="menu-block">
			<view class="title">
				商家信息
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">名称</view>
				</view>
				<input v-model="form.businessName" placeholder="请填写店铺名称" class="info-crl" maxlength="20" type="text" />
			</view>
			<view class="info-list1">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">Logo(点击图片上传)</view>
				</view>
				<image @click="chooseImage" :src="cloudStorage+form.logo" class="photo" v-if="form.logo"></image>
				<image @click="chooseImage" src="@/static/logo.png" class="photo" v-else></image>
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">营业开始时间</view>
				</view>
				<picker mode="time" :value="time" @change="bindTimeChange" class="info-crl"
					placeholder-class="inputPlaceholder">
					<view class="uni-input">{{form.startTime}}</view>
				</picker>
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">营业结束时间</view>
				</view>
				<picker mode="time" :value="time" @change="bindEndTimeChange" class="info-crl" :start="form.startTime"
					placeholder-class="inputPlaceholder">
					<view class="uni-input">{{form.endTime}}</view>
				</picker>
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">门店面积</view>
				</view>
				<input v-model="form.area" placeholder="请填写门店面积" class="info-crl" maxlength="6" type="number" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">座机号</view>
				</view>
				<input v-model="form.landlineNumber" placeholder="请填写座机号" class="info-crl" maxlength="11" type="text" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">所在区域</view>
				</view>
				<view class="form-content">
					<view class="form-ctrl" v-if="cityInfo.city" @click="navigate('/page_other/region/index?type=2')">
						{{cityInfo.city}}-{{areaInfo.area}}
					</view>
					<view class="form-ctrl" v-else @click="navigate('/page_other/region/index?type=2')">请选择所在区域</view>
				</view>
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">地址</view>
				</view>
				<view class="form-content">
					<input v-model="form.address" @click="chosenPosition" type="text" placeholder="请选择地址"
						disabled="true" placeholder-class="inputPlaceholder" class="form-ctrl" />
					<image src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/more.png"
						class="arrow"></image>
				</view>
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">详细地址</view>
				</view>
				<input v-model="form.detailedAddress" placeholder="请填写详细地址" class="info-crl" maxlength="20"
					type="text" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">主营业务</view>
				</view>
				<view class="form-content">
					<picker class="form-ctrl" :range="industry" range-key="name" :value="industryIndex"
						@change="bindIndustry">
						{{industryIndex==-1 ? "请选择主营业务" : industry[industryIndex]['name']}}
					</picker>
					<image src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/more.png"
						class="arrow"></image>
				</view>
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">门店介绍</view>
				</view>
				<textarea v-model="form.storeIntroduction" placeholder="请填写门店介绍" class="info-crl" maxlength="400"
					type="text" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="name">月销售额（元）</view>
				</view>
				<input v-model="form.monthlySales" placeholder="请填写月销售额" class="info-crl" maxlength="8" type="number" />
			</view>
		</view>
		
		<view class="agree-block">
			<checkbox-group @change="onAgreeChange">
				<checkbox :checked="isChecked"  style="font-size: 30rpx;">我已阅读并同意<span
						style="text-decoration: underline;" 
						@click="navigate('/page_other/mine/article?code='+'Merchant')">《商户入驻协议》</span></checkbox>
			</checkbox-group>
		</view>
		
		<view class="step" @click="handleSave">提交</view>
	</view>
</template>

<script>
	import {
		addAndUpdateBusinessSettled,
		getIndustryApi
	} from '@/api/bus.js';
	import {
		showToast
	} from '@/util/util.js'
	import {
		validate
	} from '@/util/validate.js'
	import {
		uploadFile
	} from '@/api/file.js'
	export default {
		data() {
			return {
				form: {
					contacts: '',
					phone: '',
					businessName: '',
					address: '',
					latitude: null,
					longitude: null,
					industryIndex: 1,
					isSale: '',
					saleIndex: 1,
					monthlySales: '',
					logo: '',
					startTime: '请选择开始时间',
					endTime: '请选择结束时间',
					businessTime: '',
					area: '',
					detailedAddress: '',
					storeIntroduction: '',
					landlineNumber: '',
					cityId: 0,
					areaId: 0
				},
				type: 1,
				rules: {
					contacts: {
						name: '联系人姓名',
						required: true
					},
					phone: {
						name: '联系电话',
						required: true,
						validator: function(value, form, callback) {
							const phoneReg =
								/^((\+|00)86)?((134\d{4})|((13[0-3|5-9]|14[1|5-9]|15[0-9]|16[2|5|6|7]|17[0-8]|18[0-9]|19[0-2|5-9])\d{8}))$/;

							if (value.length != 11) {
								callback('请输入正确的手机号');
							} else if (!phoneReg.test(value)) {
								callback('请输入正确的手机号');
							}
							callback();
						}
					},
					businessName: {
						name: '店铺名称',
						required: true
					},
					logo: {
						name: 'Logo',
						required: true
					},
					startTime: {
						name: '开始时间',
						required: true
					},
					endtTime: {
						name: '结束时间',
						required: true
					},
					area: {
						name: '门店面积',
						required: true
					},
					landlineNumber: {
						name: '座机号',
						required: true
					},
					areaId: {
						name: '所在区域',
						required: true
					},
					address: {
						name: '地址',
						required: true
					},
					detailedAddress: {
						name: '详细地址',
						required: true
					},
					storeIntroduction: {
						name: '门店介绍',
						required: true
					},
				},
				id: null,
				industry: [],
				industryIndex: -1,
				areaInfo: {
					area: ''
				},
				cityInfo: {
					city: ''
				}
			}
		},
		methods: {
			// 新增：处理用户同意状态变化
			onAgreeChange(e) {
				
				this.isChecked = !this.isChecked;
			},
			bindTimeChange(e) {
				this.form.startTime = e.detail.value
			},
			bindEndTimeChange(e) {
				this.form.endTime = e.detail.value
			},
			//所属行业
			bindIndustry(e) {
				this.industryIndex = e.detail.value;
				this.form.type = this.industry[this.industryIndex].id
			},
			getIndustry() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getIndustryApi({}).then((res) => {
					this.industry = res
				})
			},
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			chosenPosition() {
				//获取当前位置
				var that = this;
				const currentLocation = uni.getLocation({
					isHighAccuracy: true,
					success(current) {
						const currentLatitude = current.latitude;
						const currentLongitude = current.longitude;
						uni.chooseLocation({
							latitude: currentLatitude,
							longitude: currentLongitude,
							success: (choosen) => {
								delete choosen.errMsg;
								that.form.address = choosen.address;
								that.form.latitude = choosen.latitude;
								that.form.longitude = choosen.longitude;
							},
							fail: (err) => {
								showToast('选择位置失败，请重试！');
							}
						})
					},
					fail: (e) => {
						showToast('获取位置失败，请重试！');
					}
				})
			},
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (e) => {
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uploadFile(e.tempFilePaths[0]).then((res) => {
							uni.hideLoading()
							this.form.logo = res.url

						}).catch((res) => {
							uni.hideLoading()
							this.uls.showToast('上传失败');
						})
					}
				})
			},
			handleSave() {
				
				if (!this.isChecked) {
					uni.showToast({
						title: '请先阅读并同意商家入驻协议',
						icon: 'none',
					});
					return;
				}
				
				let form = JSON.parse(JSON.stringify(this.form));
				form.businessTime = form.startTime + '-' + form.endTime
				if (validate(form, this.rules)) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					addAndUpdateBusinessSettled(form).then((res) => {
						this.showToast(res, () => {
							uni.navigateBack()
						}, 1000)
					})
				}
			}

		},
		onLoad(e) {
			this.getIndustry()
			uni.$on('selectArea', (selectData) => {
				this.areaInfo = selectData;
				this.form.areaId = selectData.areaId
			})
			uni.$on('cityItem', (selectCity) => {
				this.cityInfo = selectCity;
				this.form.cityId = selectCity.cityId
			})
		},
		onReachBottom() {}
	}
</script>

<style lang="scss">
	page {
		background-color: #fafafa;
	}

	.content {
		width: 100%;
		padding: 30rpx;
		
		.agree-block {
			width: 92%;
			position: relative;
			margin: 30rpx auto;
		
		}
		
		.agree-checkbox {
			margin-top: 20rpx;
		}
		
		.step {
			width: 70%;
			height: 100rpx;
			display: flex;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
			color: #fff;
			margin: 40rpx auto;
			border-radius: 60rpx;
			justify-content: center;
			align-items: center;
		}

		.menu-block {
			background: #fff;
			border-radius: 12px;
			padding: 30rpx;
			margin-bottom: 20rpx;

			.title {
				font-size: 32rpx;
				font-weight: normal;
				line-height: 100rpx;
				display: flex;
				align-items: center;
				letter-spacing: 0em;
				color: #FF2F34;
				font-family: FZHanZhenGuangBiaoS-GB;
			}

			.info-list1 {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.info-item {
					display: flex;
					align-items: center;

					.must {
						color: #EA0000;
					}
				}

				.photo {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					margin-top: 10rpx;
				}
			}

			.info-list:last-child {
				border-bottom: none;
			}

			.info-list {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #F7F7F7;
				padding-bottom: 20rpx;
				margin-top: 20rpx;

				.img-item {
					display: flex;
					margin-top: 20rpx;

					.obverse-item {
						width: calc((100% - 30rpx) /2);
						margin-right: 30rpx;

						.upload-img {
							width: 100%;
							border-radius: 10rpx;
							height: 200rpx !important;
						}
					}

					.obverse-item:last-child {
						margin-right: 0;
					}

				}

				.info-item {
					display: flex;
					align-items: center;

					.must {
						color: #EA0000;
					}

					.formwork {
						color: #027BFF;
					}
				}

				.upload {
					display: flex;
					flex-direction: row;
					flex-wrap: wrap;

					.image-item {
						margin-top: 20rpx;
						margin-right: 20rpx;
						display: flex;
						flex-direction: row;
						// box-sizing: border-box;
						// border: 1px solid #efefef;

						.img {
							width: 190rpx;
							height: 200rpx;
							border-radius: 20rpx;
						}

						.iconfont {
							.delete {
								width: 20px;
								height: 20px;
								position: absolute;
								margin-left: -30rpx;
							}
						}
					}

					.image-item:nth-child(3n) {
						margin-right: 0;
					}

					.delete-item {
						width: 100%;
						display: flex;
						justify-content: flex-end;

						.delete {
							width: 20px;
							height: 20px;
							position: absolute;
						}
					}

					.image-item1 {
						display: flex;
						flex-direction: row;

						.delete {
							margin-top: 10rpx;
							margin-left: -40rpx;
							width: 20px;
							height: 20px;
							position: absolute;
						}
					}

					.upload-content {
						position: relative;
						background: #fafafa;
						border-radius: 5px;
						width: 200rpx;
						height: 200rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						margin-top: 20rpx;

						.title {
							position: absolute;
							top: 50%;
							left: 50%;
						}

						.upload {
							width: 90rpx;
							height: 90rpx;
						}
					}
				}

				.form-content {
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-between;
					margin-top: 20rpx;

					.check {
						transform: scale(0.7);
					}

					.form-ctrl {
						height: 80rpx;
						line-height: 80rpx;
						flex-grow: 1;
						color: #767676;
					}

					.arrow {
						flex-grow: 0;
						width: 40rpx;
						height: 40rpx;
					}
				}

				.radio-list {
					.radio-item {
						margin-top: 30rpx;

						.check {
							transform: scale(0.7);
						}
					}
				}

				.info-crl {
					margin-top: 30rpx;
				}
			}
		}
	}
</style>